"use strict";
cc._RF.push(module, 'c63c5oIiCRPLaC81YD0U0on', 'gameManager');
// scripts/engines/managers/gameManager.ts

"use strict";
/**
 * 游戏管理类
 */
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
Object.defineProperty(exports, "__esModule", { value: true });
var superManager_1 = require("./superManager");
var gameMode_json_1 = require("./gameContent/gameMode_json");
var gameEngine_1 = require("../gameEngine");
var data_control_1 = require("../libs/data_control");
var gameConfig_1 = require("../configs/gameConfig");
var versionNotice_1 = require("../../O1ModuleRes/frame/versionNotice");
var GameManager = /** @class */ (function (_super) {
    __extends(GameManager, _super);
    function GameManager() {
        var _this = _super.call(this) || this;
        //
        _this.className = '游戏管理';
        //
        _this.m_currGameMode = null;
        //游戏公告
        _this.m_versionNotice = null;
        //游戏公告KEY
        _this.m_VERSION_NOTICE_KEY = 'game-version-notice';
        return _this;
    }
    GameManager.prototype.loadManager = function () {
        return __awaiter(this, void 0, Promise, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, _super.prototype.loadManager.call(this)];
                    case 1:
                        _a.sent();
                        //
                        this.m_versionNotice = gameEngine_1.default.getServices().localStorageSrv.getStorage(this.m_VERSION_NOTICE_KEY);
                        return [2 /*return*/, Promise.resolve()];
                }
            });
        });
    };
    //游戏首次进入
    GameManager.prototype.onGameRun = function () {
        //
        var currVersion = gameConfig_1.default.VERSION_NOTICE;
        //
        if (this.m_versionNotice != currVersion) {
            //
            this.m_versionNotice = currVersion;
            gameEngine_1.default.getServices().localStorageSrv.setStorage(this.m_VERSION_NOTICE_KEY, this.m_versionNotice);
            //
            this.showVersionNotice();
        }
        var tt = window['tt'];
        if (tt) {
            var options = tt.getLaunchOptionsSync();
            console.log(options.showFrom);
            console.log('启动参数', options, JSON.stringify(options));
        }
    };
    GameManager.prototype.showVersionNotice = function () {
        var _this = this;
        if (this.m_versionNotice) {
            gameEngine_1.modules().uiControlModule.showFramePrefab('preload/frame/versionNotice', null, false, function (node) {
                node.getComponent(versionNotice_1.default).setNotice(_this.m_versionNotice);
            });
        }
        // this.m_versionNotice && O1HD.getModules().uiControlModule.showDialogBox('版本公告', this.m_versionNotice);
    };
    GameManager.prototype.getVersionNotice = function () {
        return this.m_versionNotice;
    };
    GameManager.prototype.loadModeOver = function (isShowLoading) {
        if (isShowLoading === void 0) { isShowLoading = true; }
        return __awaiter(this, void 0, void 0, function () {
            var _a;
            var _this = this;
            return __generator(this, function (_b) {
                switch (_b.label) {
                    case 0:
                        console.log('加载子游戏场景', this.m_currGameMode.sceneName);
                        if (!isShowLoading) return [3 /*break*/, 2];
                        return [4 /*yield*/, gameEngine_1.default.getModules().uiControlModule.showLoading()];
                    case 1:
                        _b.sent();
                        _b.label = 2;
                    case 2:
                        _a = this.m_currGameMode.sceneMode;
                        switch (_a) {
                            case gameMode_json_1.SCENE_MODE.FIRE: return [3 /*break*/, 3];
                            case gameMode_json_1.SCENE_MODE.PREFAB: return [3 /*break*/, 5];
                        }
                        return [3 /*break*/, 6];
                    case 3: return [4 /*yield*/, new Promise(function (resolve, reject) {
                            gameEngine_1.default.loadScene(_this.m_currGameMode.sceneName, function () {
                                resolve();
                                // O1HD.getModules().uiControlModule.hideLoading();
                            });
                        })];
                    case 4:
                        _b.sent();
                        return [3 /*break*/, 7];
                    case 5:
                        {
                            this.m_currGameMode.bundle.load(this.m_currGameMode.sceneName, cc.Prefab, function (error, assets) {
                                if (error) {
                                    console.error('#55 模式对应预制体不存在或异常', _this.m_currGameMode);
                                    return;
                                }
                                _this.m_currGameMode.currScenePrefabNode = cc.instantiate(assets);
                                gameEngine_1.default.Canvas().addChild(_this.m_currGameMode.currScenePrefabNode);
                                gameEngine_1.default.getModules().uiControlModule.hideLoading();
                            });
                            return [3 /*break*/, 7];
                        }
                        _b.label = 6;
                    case 6:
                        {
                            gameEngine_1.default.getModules().uiControlModule.hideLoading();
                            console.error('#56 请指示加载模式的类型');
                            return [3 /*break*/, 7];
                        }
                        _b.label = 7;
                    case 7: return [2 /*return*/];
                }
            });
        });
    };
    //获取主场景名
    GameManager.prototype.getMainSceneNameAndLoadBundle = function () {
        //
        //
        // for (let mode of gameMode_json()) {
        //     if (mode.isMainScene) {
        //         //是场景
        //         if (mode.sceneMode == SCENE_MODE.FIRE) {
        //             this.m_currGameMode = mode;
        //             //加载子包
        //             return mode;
        //         } else {
        //             console.error('入口场景模式不允许为预制体', mode.name, mode);
        //             return null;
        //         }
        //     }
        // }
        return 'hall';
    };
    GameManager.prototype.setMainModeLoaded = function (mode) {
        this.m_currGameMode = mode;
    };
    //选择模式
    GameManager.prototype.selectMode = function (mode, isShowLoading) {
        return __awaiter(this, void 0, void 0, function () {
            var currMode, bundle;
            var _this = this;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        currMode = null;
                        //
                        currMode = gameMode_json_1.default()[mode];
                        if (currMode == null) {
                            gameEngine_1.default.getModules().uiControlModule.showMessageBox("\u656C\u8BF7\u671F\u5F85" + mode);
                            return [2 /*return*/];
                        }
                        this.m_currGameMode = currMode;
                        //
                        //在其他地方已经加载子包
                        if (currMode.bundle == null) {
                            console.debug('子包是否已加载', currMode.name, cc.assetManager.bundles.get(currMode.name) != null);
                            bundle = cc.assetManager.bundles.get(currMode.name);
                            if (bundle) {
                                currMode.bundle = bundle;
                            }
                        }
                        if (!(currMode.bundle == null)) return [3 /*break*/, 2];
                        console.log('显示进度');
                        // O1HD.getModules().uiControlModule.showLoading();
                        console.log('进度显示完', currMode, mode);
                        return [4 /*yield*/, new Promise(function (resolve, reject) {
                                cc.assetManager.loadBundle(currMode.name, function (err, bundle) { return __awaiter(_this, void 0, void 0, function () {
                                    return __generator(this, function (_a) {
                                        switch (_a.label) {
                                            case 0:
                                                // O1HD.getModules().uiControlModule.hideLoading();
                                                if (err) {
                                                    console.error('游戏子包不存在', mode);
                                                    return [2 /*return*/];
                                                }
                                                currMode.bundle = bundle;
                                                gameEngine_1.managers().soundMgr.addOneSoundBundle(currMode.name, bundle);
                                                return [4 /*yield*/, this.loadModeOver(isShowLoading)];
                                            case 1:
                                                _a.sent();
                                                resolve();
                                                return [2 /*return*/];
                                        }
                                    });
                                }); });
                            })];
                    case 1:
                        _a.sent();
                        return [3 /*break*/, 4];
                    case 2: 
                    //
                    return [4 /*yield*/, this.loadModeOver(isShowLoading)];
                    case 3:
                        //
                        _a.sent();
                        _a.label = 4;
                    case 4: return [2 /*return*/];
                }
            });
        });
    };
    GameManager.prototype.getCurrMode = function () {
        return this.m_currGameMode;
    };
    GameManager.prototype.getCurrModeNeedPower = function () {
        var type = this.m_currGameMode.type;
        return this.getModeNeedPower(type);
    };
    GameManager.prototype.getModeNeedPower = function (mode) {
        //TODO:获取对应体力 对照oss根据type映射
        return 0;
    };
    GameManager.prototype.getModeInfo = function (mode) {
        var md = gameMode_json_1.default()[mode];
        if (md && md.lookVideCount > 0) {
            md.currLookedVideoCount =
                Number(gameEngine_1.default.getServices().localStorageSrv.getStorage("mode-looked-" + md.type) || '');
            md.lookVideoUnlockDate =
                gameEngine_1.default.getServices().localStorageSrv.getStorage("mode-looked-date-" + md.type) || '';
            //
        }
        return md;
    };
    GameManager.prototype.addModeLookCount = function (mode) {
        var md = gameMode_json_1.default()[mode];
        if (md.lookVideCount > 0) {
            md.currLookedVideoCount++;
            gameEngine_1.default.getServices().localStorageSrv.setStorage("mode-looked-" + md.type, md.currLookedVideoCount);
            gameEngine_1.default.getServices().localStorageSrv.setStorage("mode-looked-date-" + md.type, data_control_1.default.getCurrDateDayString());
        }
    };
    //返回大厅
    GameManager.prototype.returnMainScene = function () {
        return __awaiter(this, void 0, void 0, function () {
            var _a;
            return __generator(this, function (_b) {
                switch (_b.label) {
                    case 0: return [4 /*yield*/, gameEngine_1.default.getModules().uiControlModule.showLoading()];
                    case 1:
                        _b.sent();
                        _a = this.m_currGameMode.sceneMode;
                        switch (_a) {
                            case gameMode_json_1.SCENE_MODE.PREFAB: return [3 /*break*/, 2];
                            case gameMode_json_1.SCENE_MODE.FIRE: return [3 /*break*/, 3];
                        }
                        return [3 /*break*/, 5];
                    case 2:
                        {
                            if (this.m_currGameMode.currScenePrefabNode && this.m_currGameMode.currScenePrefabNode.isValid) {
                                this.m_currGameMode.currScenePrefabNode.destroy();
                            }
                            this.m_currGameMode.currScenePrefabNode = null;
                            //
                            gameEngine_1.default.getModules().uiControlModule.hideLoading();
                            return [3 /*break*/, 5];
                        }
                        _b.label = 3;
                    case 3: 
                    //
                    return [4 /*yield*/, new Promise(function (resolve, reject) {
                            gameEngine_1.default.loadScene('loading', resolve);
                        })];
                    case 4:
                        //
                        _b.sent();
                        gameEngine_1.default.getModules().uiControlModule.hideLoading();
                        return [3 /*break*/, 5];
                    case 5: return [2 /*return*/];
                }
            });
        });
    };
    return GameManager;
}(superManager_1.default));
exports.default = GameManager;

cc._RF.pop();